package sany.iec.service.impl;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import sany.iec.config.GlobalVariable;
import sany.iec.entity.DataEntity;
import sany.iec.entity.ServerEntity;
import sany.iec.enums.ModbusRegisterEnum;
import sany.iec.service.ModbusServerService;
import sany.iec.service.UpdateDataService;

import javax.annotation.Resource;

/**
 * @Author : 托马斯
 * @Date : 2025/9/19 15:39
 * @Description :
 */
@Service
public class UpdateDataServiceImpl implements UpdateDataService {

    @Resource
    private ModbusServerService modbusServerService;

    @Override
    public String updateModbusData(ServerEntity server, String key, Float value) throws InterruptedException {
        if (StringUtils.isBlank(key) || server == null || value == null ) {
            return "数据有误。";
        }
        DataEntity point = null;
        for (DataEntity listDatum : server.getListData()) {
            if (listDatum.getKey().equals(key)) {
                point = listDatum;
                break;
            }
        }
        if (point == null) {
            return "不存在该key对应的点。";
        }
        for (int i = 0; i < 12; i++) {
            if (point.getModbusRegisterEnum().equals(ModbusRegisterEnum.COIL)) {
                boolean v1 = ObjectUtils.defaultIfNull(value, 0.0f) == 1.0f;
                if (server.getModbusServerSet() != null) {
                    modbusServerService.writeCoil(server, point, v1);
                }
                GlobalVariable.GLOBAL_MAP.put(server.getPort() + ":" + point.getKey(), v1 ? 1.0f : 0.0f);
            } else if (point.getModbusRegisterEnum().equals(ModbusRegisterEnum.INPUT)) {
                boolean v1 = ObjectUtils.defaultIfNull(value, 0.0f) == 1.0f;
                if (server.getModbusServerSet() != null) {
                    modbusServerService.writeInput(server, point, v1);
                }
                GlobalVariable.GLOBAL_MAP.put(server.getPort() + ":" + point.getKey(), v1 ? 1.0f : 0.0f);
            } else if (point.getModbusRegisterEnum().equals(ModbusRegisterEnum.HOLD_REGISTER)) {
                if (server.getModbusServerSet() != null) {
                    modbusServerService.writeHoldRegister(server, point, value);
                }
                GlobalVariable.GLOBAL_MAP.put(server.getPort() + ":" + point.getKey(), value);
            } else if (point.getModbusRegisterEnum().equals(ModbusRegisterEnum.INPUT_REGISTER)) {
                if (server.getModbusServerSet() != null) {
                    modbusServerService.writeInputRegister(server, point, value);
                }
                GlobalVariable.GLOBAL_MAP.put(server.getPort() + ":" + point.getKey(), value);
            }
            Thread.sleep(100);
        }
        return "修改成功！！！";
    }

    @Override
    public String updateIEC104Data(ServerEntity server, String key, Float value) {
        if (StringUtils.isBlank(key) || server == null || value == null ) {
            return "数据有误。";
        }
        DataEntity point = null;
        for (DataEntity listDatum : server.getListData()) {
            if (listDatum.getKey().equals(key)) {
                point = listDatum;
                break;
            }
        }
        if (point == null) {
            return "不存在该key对应的点。";
        }
        GlobalVariable.GLOBAL_MAP.put(server.getPort() + ":" + point.getKey(), value);
        return "修改成功！！！";
    }
}
